package com.oa.admin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.DepartmentApplyAddDTO;
import com.oa.admin.dto.DepartmentApplyListDTO;
import com.oa.admin.entity.DepartmentalBudget;
import com.oa.admin.service.IDepartmentalBudgetService;
import com.oa.admin.vo.DepartmentApplyByDepartmentIdVO;
import com.oa.admin.vo.DepartmentApplyDetailsVO;
import com.oa.admin.vo.DepartmentApplyListVO;
import com.oa.core.PageResult;
import com.oa.admin.entity.DepartmentApply;
import com.oa.admin.mapper.DepartmentApplyMapper;
import com.oa.admin.service.IDepartmentApplyService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.core.enumm.ProcessDefineEnum;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.system.dto.ApplyApprovalAddDTO;
import com.oa.system.dto.ApprovalDTO;
import com.oa.system.entity.ApplyApproval;
import com.oa.system.entity.User;
import com.oa.system.service.IApplyApprovalService;
import com.oa.system.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 部门活动申请表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class DepartmentApplyServiceImpl extends ServiceImpl<DepartmentApplyMapper, DepartmentApply> implements IDepartmentApplyService {

    private final DepartmentApplyMapper departmentApplyMapper;

    @Autowired
    public DepartmentApplyServiceImpl(DepartmentApplyMapper departmentApplyMapper) {
        this.departmentApplyMapper = departmentApplyMapper;
    }

    @Autowired
    private IApplyApprovalService applyApprovalService;

    @Autowired
    private IDepartmentalBudgetService departmentalBudgetService;

    @Autowired
    private IUserService userService;

    @Override
    @CommonService
    public PageResult<DepartmentApply> findList(Integer pageNum, Integer pageSize) {
        if(pageNum == null){
            List<DepartmentApply> list = departmentApplyMapper.findList();
            return new PageResult<>(list);
        }else{
            // 当前页，总条数 构造 page 对象
            Page<DepartmentApply> page = new Page<>(pageNum, pageSize);
            //查询
            List<DepartmentApply> list = departmentApplyMapper.findList(page);
            return new PageResult<>(list);
        }
    }

    @CommonService
    @Override
    public List<DepartmentApplyByDepartmentIdVO> findByDepartmentId(String departmentId) {
        return departmentApplyMapper.findByDepartmentId(departmentId);
    }

    @CommonService
    @Override
    public PageResult<DepartmentApplyListVO> findDepartmentApplyList(DepartmentApplyListDTO departmentApplyListDTO) {
        return new PageResult<>(departmentApplyMapper.findDepartmentApplyList(departmentApplyListDTO.getPage(), departmentApplyListDTO));
    }


    @Transactional(rollbackFor = Exception.class)
    @CommonService
    @Override
    public Resp add(DepartmentApplyAddDTO departmentApplyAddDTO) throws DataIsEmptyException, BusinessException {
        User user = userService.selectById(departmentApplyAddDTO.getProposer());
        DepartmentalBudget departmentalBudget = departmentalBudgetService.selectOne(new EntityWrapper().eq("department_id", user.getDepartmentId()));

        if(departmentalBudget == null){
            return new Resp().failure("无部门经费记录，请联系相关管理员处理。");
        }

        double yearBudget = Double.valueOf(departmentalBudget.getYearBudget());
        double usedBudget = Double.valueOf(departmentalBudget.getUsedBudget());
        double activityBudget = Double.valueOf(departmentApplyAddDTO.getActivityBudget());

        if(usedBudget + activityBudget > yearBudget){
            return new Resp().failure("申请的经费加上已用经费不能大于年度经费");
        }

        DepartmentApply departmentApply = new DepartmentApply();
        BeanUtils.copyProperties(departmentApplyAddDTO, departmentApply);
        if(insert(departmentApply)){
            ApplyApprovalAddDTO applyApprovalAddDTO = new ApplyApprovalAddDTO();
            applyApprovalAddDTO.setApplyId(departmentApply.getId());
            applyApprovalAddDTO.setApprovalProcessId(ProcessDefineEnum.DEPARTMENTAL_ACTIVITIES.getId());
            applyApprovalAddDTO.setApplyApprovalName(user.getName() + ProcessDefineEnum.DEPARTMENTAL_ACTIVITIES.getText());
            applyApprovalAddDTO.setApprovalUserId(user.getId());
            return applyApprovalService.addApproval(applyApprovalAddDTO);
        }
        return new Resp().failure();
    }

    @CommonService
    @Override
    public DepartmentApplyDetailsVO findDepartmentApplyDetails(String id) {
        return departmentApplyMapper.findDepartmentApplyDetails(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @CommonService
    @Override
    public Resp audit(String id, Integer status) throws DataIsEmptyException {
        ApprovalDTO approvalDTO = new ApprovalDTO();
        approvalDTO.setId(Long.valueOf(id));
        approvalDTO.setUserId("1");
        approvalDTO.setStatus(status);
        return applyApprovalService.audit(approvalDTO, d -> {
            DepartmentApply departmentApply = selectById(d.getApplyApproval().getApplyId());
            double activityBudget = Double.parseDouble(departmentApply.getActivityBudget());
            User user = userService.selectById(departmentApply.getProposer());
            DepartmentalBudget departmentalBudget = departmentalBudgetService.selectOne(new EntityWrapper().eq("department_id", user.getDepartmentId()));
            double usedBudget = Double.parseDouble(departmentalBudget.getUsedBudget() == null ? "0": departmentalBudget.getUsedBudget());
            double result = usedBudget + activityBudget;
            departmentalBudget.setUsedBudget(String.valueOf(result));
            departmentalBudgetService.updateById(departmentalBudget);
        });
    }
}
